//
//  NQUserInfoVc.m
//  ZLFastDoctor
//
//  Created by 李岚清 on 16/5/22.
//  Copyright © 2016年 LS. All rights reserved.
//

#import "NQUserInfoVc.h"
#import "NQPickerView.h"
#import "NQUserInfoCell.h"
#import "NQProvinceSelectView.h"
#import "NQUserInfo.h"
#import "UIImage+scallImage.h"
#import "NQSaveTool.h"

#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS
#import <Masonry/Masonry.h>
//cell重用标识
static NSString *NQUserInfoVcIdentifier = @"NQUserInfoVcIdentifier";

@interface NQUserInfoVc () <UINavigationControllerDelegate, UIImagePickerControllerDelegate,NQPickerViewDelegate,NQProvinceSelectViewDelegate>
/**
 *  头部View
 */
@property (nonatomic, strong) UIView *headerView;
/**
 *  身高按钮
 */
@property (nonatomic, strong) UIButton *heightBtn;
/**
 *  体重按钮
 */
@property (nonatomic, strong) UIButton *weightBtn;
/**
 *  省份按钮
 */
@property (nonatomic, strong) UIButton *proVinBtn;
//******************************头部View的子控件********************************************
/* 头像 */
@property (nonatomic, strong) UIImageView *iconView;
/* 名字View */
@property (nonatomic, strong) UIImageView *nameView;
/* idView */
@property (nonatomic, strong) UIImageView *idView;
/* 手机View */
@property (nonatomic, strong) UIImageView *iphoneView;
/* 名字 */
@property (nonatomic, strong) UITextField *nameField;
/* id号 */
@property (nonatomic, strong) UITextField *idField;
/* 手机号 */
@property (nonatomic, strong) UITextField *iphoneField;

/**
 *  文字数据源数组
 */
@property (nonatomic, strong) NSArray *titleArr;
/**
 *  遮盖
 */
@property (nonatomic, strong) UIView *coverView;
/**
 *  身高
 */
@property (nonatomic, copy) NSString *height;
/**
 *  体重
 */
@property (nonatomic, copy) NSString *weight;
/**
 *  省份
 */
@property (nonatomic, copy) NSString *province;

@end

@implementation NQUserInfoVc
#pragma mark - set/get
- (void)setUserInfo:(NQUserInfo *)userInfo{
    _userInfo = userInfo;
    //赋值数据
//    self.iconView.image = userInfo.iconImage;
//    self.nameField.text = userInfo.name;
//    self.idField.text = userInfo.ID;
//    self.iphoneField.text = userInfo.iphone;
//    self.height = userInfo.height;
//    self.weight = userInfo.weight;
//    self.province = userInfo.province;
//    //刷新界面
//    [self.tableView reloadData];
}

#pragma mark - 懒加载遮盖View
- (UIView *)coverView{
    if (!_coverView ) {
        _coverView = [[UIView alloc] init];
        _coverView.frame = NQScreenBounds;
        _coverView.backgroundColor = [UIColor blackColor];
        _coverView.alpha = 0.5;
    }
    return _coverView;
}

#pragma mark - 事件传递 -- 不进来
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

//关闭键盘
- (void)closeKeyBoard{
    [self.view endEditing:YES];
}

#pragma mark - 生命周期方法
- (void)viewDidLoad {
    [super viewDidLoad];
    //基本设置
    [self setupBaseSetting];
    
    //给头部view添加手势,关闭键盘
    self.headerView.userInteractionEnabled = YES;
    //添加点按手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBoard)];
    [self.headerView addGestureRecognizer:tap];
    
    if (self.userInfo) {
        //赋值数据
        self.iconView.image = self.userInfo.iconImage;
        self.nameField.text = self.userInfo.name;
        self.idField.text = self.userInfo.ID;
        if ([NQSaveTool objectForKey:NQiphone]) {
            self.iphoneField.text = [NQSaveTool objectForKey:NQiphone];
        }else{
            self.iphoneField.text = self.userInfo.iphone;
        }
        self.height = self.userInfo.height;
        self.weight = self.userInfo.weight;
        self.province = self.userInfo.province;
        //刷新界面
        [self.tableView reloadData];
    }
}

//基本设置
- (void)setupBaseSetting{
    //重写返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_button_back_01"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    //设置头部headerView
    self.tableView.tableHeaderView = [self setHeaderView];
    //设置底部footerView
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"个人信息修改";
    //注册cell
    [self.tableView registerClass:[NQUserInfoCell class] forCellReuseIdentifier:NQUserInfoVcIdentifier];
    
    //给iconView添加一个手势
    self.iconView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(alertTip)];
    [self.iconView addGestureRecognizer:tap];
}

//重写返回按钮,判断是否打开菜单侧滑
- (void)back{
    //创建模型
    NQUserInfo *userInfo = [NQUserInfo userInfoWithIconImage:self.iconView.image name:self.nameField.text ID:self.idField.text iphone:self.iphoneField.text height:self.height weight:self.weight province:self.province];
    //归档用户数据模型
    [NQUserInfo saveUserInfo:userInfo];
    //把改变了的数据模型传递给NQLeftMenuVc
    NQLeftMenuVc *leftVc = (NQLeftMenuVc *)NQDrawer.leftViewController;
    leftVc.userInfo = userInfo;
    //跳转回上一层
    [self.navigationController popViewControllerAnimated:YES];
    [NQDrawer open];
}

#pragma mark - 弹框提示
- (void)alertTip{
    //1.弹出提示框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"头像设置" message:@"请选择修改头像的方式" preferredStyle:UIAlertControllerStyleActionSheet];
    
    //2.添加按钮
    //从本地相册添加
    UIAlertAction *photoLibrary = [UIAlertAction actionWithTitle:@"本地相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self chooesIcon];
    }];
    [alert addAction:photoLibrary];
    //拍照添加
    UIAlertAction *Camera = [UIAlertAction actionWithTitle:@"拍摄照片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self chooseCamera];
    }];
    [alert addAction:Camera];
    //取消按钮
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:cancel];
    
    //3.显示消息框
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - 拍摄照片
- (void)chooseCamera{
    //创建图片选择器
    UIImagePickerController *picVc = [[UIImagePickerController alloc] init];
    //先检查相机是否可用
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        //设置跳转类型
        picVc.sourceType = UIImagePickerControllerSourceTypeCamera;
        //设置代理
        picVc.delegate = self;
        //进行跳转
        [self.navigationController presentViewController:picVc animated:YES completion:nil];
    }
}

#pragma mark - 选择头像图片
- (void)chooesIcon{
    //创建图片选择器
    UIImagePickerController *picVc = [[UIImagePickerController alloc] init];
    //设置跳转类型 -- 所有相册
    picVc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    //设置代理
    picVc.delegate = self;
    //设置选择后的图片可被编辑
    picVc.allowsEditing = YES;
    //进行跳转
    [self presentViewController:picVc animated:YES completion:nil];
}

//相册的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo{
    //选择图片之后,把图片设置到iconView上
    //绘制规定范围内的图片 -- 减小内存
//     NSLog(@"%@",image);//<UIImage: 0x13d8e64e0> size {3024, 4032} orientation 3 scale 1.000000
    UIImage *img = [UIImage getScallImage:image scale:CGSizeMake(100, 100)];
//    NSLog(@"%@",img);//<UIImage: 0x13dca39d0> size {100, 100} orientation 0 scale 1.000000
    self.iconView.image = img;
    
    //实现dismiss方法
    [picker dismissViewControllerAnimated:NO completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    //实现dismiss方法
    [picker dismissViewControllerAnimated:NO completion:nil];
}


#pragma mark - UITableView数据源方法
//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//每组行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

//单元格内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //1.创建单元格
    NQUserInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:NQUserInfoVcIdentifier];
    
    //2.赋值模型数据
    if (indexPath.row == 0) {
        cell.infoType = @"身高";
        cell.infoResult = self.height;
    }else if (indexPath.row == 1){
        cell.infoType = @"体重";
        cell.infoResult = self.weight;
    }else if (indexPath.row == 2){
        cell.infoType = @"省份";
        cell.infoResult = self.province;
    }
    
    //3.返回单元格
    return cell;
}

#pragma mark - UITableView代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.view endEditing:YES];
    //禁止滚动
    self.tableView.scrollEnabled = NO;
    //显示遮罩
    [self.view addSubview:self.coverView];
    
    if (indexPath.row == 0) {
//        NSLog(@"点击了身高");
        NQPickerView *pickerView = [NQPickerView nq_pickerViewWithType:@"身高" unit:@"cm"];
        pickerView.delegate = self;
        [self.view addSubview:pickerView];
    }else if (indexPath.row == 1){
        NQPickerView *pickerView = [NQPickerView nq_pickerViewWithType:@"体重" unit:@"kg"];
        pickerView.delegate = self;
        [self.view addSubview:pickerView];
//        NSLog(@"点击了体重");
    }else if (indexPath.row == 2){
//        NSLog(@"点击了省份");
        NQProvinceSelectView *provinceView = [[NQProvinceSelectView alloc] init];
        provinceView.delegate = self;
        [self.view addSubview:provinceView];
    }
}

#pragma amrk - NQPickerViewDelegate
- (void)pickerViewDidClickBtn:(NQPickerBtnType)type result:(NSString *)Result pickerViewtypeName:(NSString *)typeName{
    [self.coverView removeFromSuperview];
    if (type == NQPickerBtnTypePost) {
        if ([typeName isEqualToString:@"身高"]) {
//            NSLog(@"%@",Result);
            self.height = Result;
        }else if ([typeName isEqualToString:@"体重"]){
//            NSLog(@"%@",Result);
            self.weight = Result;
        }
        [self.tableView reloadData];
    }
}

#pragma mark - NQProvinceSelectViewDelegate
- (void)provinceSelectViewDidClickProvince:(NQProvinceBtnType)type provinceName:(NSString *)provinceName{
    [self.coverView removeFromSuperview];
    if (type == NQPickerBtnTypePost) {
        self.province = provinceName;
    }
    [self.tableView reloadData];
}

#pragma mark - 懒加载子控件
- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] init];
        _headerView.backgroundColor = [UIColor whiteColor];
        _headerView.frame = CGRectMake(0, 0, 0, 150);
    }
    return _headerView;
}

- (UIButton *)heightBtn{
    if (!_heightBtn) {
        _heightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _heightBtn.backgroundColor = [UIColor redColor];
    }
    return _heightBtn;
}

- (UIButton *)weightBtn{
    if (!_weightBtn) {
        _weightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _weightBtn.backgroundColor = [UIColor blackColor];
    }
    return _weightBtn;
}

- (UIButton *)proVinBtn{
    if (!_proVinBtn) {
        _proVinBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _proVinBtn.backgroundColor = [UIColor greenColor];
    }
    return _proVinBtn;
}


#pragma mark - 懒加载头部子控件
- (UITextField *)nameField{
    if (!_nameField) {
        _nameField = [[UITextField alloc] init];
        _nameField.text = @"凤凰涅槃";
    }
    return _nameField;
}
- (UITextField *)idField{
    if (!_idField) {
        _idField = [[UITextField alloc] init];
        _idField.text = @"520520520520520";
    }
    return _idField;
}
- (UITextField *)iphoneField{
    if (!_iphoneField) {
        _iphoneField = [[UITextField alloc] init];
        if ([NQSaveTool objectForKey:NQiphone]) {
             _iphoneField.text = [NQSaveTool objectForKey:NQiphone];
        }else{
            _iphoneField.text = @"10086";
        }
    }
    return _iphoneField;
}

- (UIImageView *)iconView{
    if (!_iconView) {
        _iconView = [[UIImageView alloc] init] ;
        _iconView.image = [UIImage imageNamed:@"name"];
        _iconView.layer.cornerRadius = 50;
        _iconView.layer.masksToBounds = YES;
    }
    return _iconView;
}

- (UIImageView *)nameView{
    if (!_nameView) {
        _nameView = [[UIImageView alloc] init];
        _nameView.image = [UIImage imageNamed:@"0"];
    }
    return _nameView;
}

- (UIImageView *)idView{
    if (!_idView) {
        _idView = [[UIImageView alloc] init];
        _idView.image = [UIImage imageNamed:@"idCard"];
    }
    return _idView;
}

- (UIImageView *)iphoneView{
    if (!_iphoneView) {
        _iphoneView = [[UIImageView alloc] init];
        _iphoneView.image = [UIImage imageNamed:@"login_phone"];
    }
    return _iphoneView;
}
#pragma mark - 封装头部子控件
- (UIView *)setHeaderView{
    //添加子控件
    [self.headerView addSubview:self.iconView];
    [self.headerView addSubview:self.nameView];
    [self.headerView addSubview:self.idView];
    [self.headerView addSubview:self.iphoneView];
    [self.headerView addSubview:self.nameField];
    [self.headerView addSubview:self.idField];
    [self.headerView addSubview:self.iphoneField];
    //设置子控件
    [self.iconView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerView).offset(25);
        make.left.equalTo(self.headerView).offset(25);
        make.width.height.equalTo(100);
    }];
    [self.nameView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconView.right).offset(15);
        make.top.equalTo(self.iconView);
        make.width.height.equalTo(20);
    }];
    [self.idView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconView.right).offset(15);
        make.centerY.equalTo(self.iconView);
        make.width.height.equalTo(20);
    }];
    [self.iphoneView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconView.right).offset(15);
        make.bottom.equalTo(self.iconView);
        make.width.height.equalTo(20);
    }];
    [self.nameField makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameView.right).offset(30);
        make.centerY.equalTo(self.nameView);
    }];
    [self.idField makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.idView.right).offset(30);
        make.centerY.equalTo(self.idView);
    }];
    [self.iphoneField makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iphoneView.right).offset(30);
        make.centerY.equalTo(self.iphoneView);
    }];
    return self.headerView;
}




@end
